package com.rdc.qingkou.ui;


import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.widget.ImageButton;
import android.widget.Toast;

import com.githang.statusbar.StatusBarCompat;
import com.rdc.qingkou.Application.MyApplication;
import com.rdc.qingkou.R;
import com.rdc.qingkou.adapter.MyFragmentStatePagerAdapter;
import com.rdc.qingkou.base.BaseActivity;
import com.rdc.qingkou.contract.UserInfoContract;
import com.rdc.qingkou.entity.ModifyUserInfoResult;
import com.rdc.qingkou.presenter.HomepagePresenter;
import com.rdc.qingkou.presenter.UserInfoPresenter;
import com.rdc.qingkou.ui.iview.IHomepageView;
import com.rdc.qingkou.utils.MyUtils;
import com.rdc.qingkou.view.fragment.HomepageFragment;
import com.rdc.qingkou.vo.CardVO;
import com.rdc.qingkou.widget.GalleryPageTransformer;
import com.rdc.qingkou.widget.MyViewPager;
import com.rdc.qingkou.widget.VpSwipeRefreshLayout;

import java.util.ArrayList;
import java.util.List;

public class HomepageActivity extends BaseActivity<HomepagePresenter> implements IHomepageView, View.OnClickListener,UserInfoContract.IView {

    private TabLayout mTabLayout;
    private MyFragmentStatePagerAdapter<HomepageFragment> mMyFragmentAdapter;
    private MyViewPager mViewPager;
    private List<HomepageFragment> mHomepageFragmentList;
    private List<Integer> mTabImageIdList;
    private List<String> mCustomTagList;
    private VpSwipeRefreshLayout swipeRefresh;

    private UserInfoPresenter mUserInfoPresenter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
        StatusBarCompat.setStatusBarColor(this, getResources().getColor(R.color.windowBackground), true);
        setContentView(R.layout.activity_homepage);
        initData();
        initView();
        swipeRefresh.setRefreshing(true);
        refreshing();
    }

    private void initData() {

        mTabImageIdList = new ArrayList<>();
        mTabImageIdList.add(R.drawable.selector_tab_subscribe);
        mTabImageIdList.add(R.drawable.selector_tab_hot);
        mTabImageIdList.add(R.drawable.selector_tab_original);
        mTabImageIdList.add(R.drawable.selector_tab_excerpt);

        mHomepageFragmentList = new ArrayList<>();
        mCustomTagList = new ArrayList<>();

    }

    private void initView() {
        swipeRefresh = (VpSwipeRefreshLayout) findViewById(R.id.homepage_swipeRefreshLayout);
        swipeRefresh.setColorSchemeResources(R.color.colorStart);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        refreshing();
                    }
                }).start();
            }
        });
        mViewPager = (MyViewPager) findViewById(R.id.homepage_viewpager);
        mMyFragmentAdapter =
                new MyFragmentStatePagerAdapter<>(getSupportFragmentManager(), mHomepageFragmentList);
        mViewPager.setAdapter(mMyFragmentAdapter);
        mViewPager.setOffscreenPageLimit(2);
        mViewPager.setPageTransformer(false, new GalleryPageTransformer());
        mViewPager.setPageMargin(-80);

        mTabLayout = (TabLayout) findViewById(R.id.homepage_tabLayout);
        mTabLayout.setSmoothScrollingEnabled(true);
        mTabLayout.setupWithViewPager(mViewPager);

        ImageButton mPersonalHomepageButton = (ImageButton) findViewById(R.id.homepage_personal_imageButton);
        mPersonalHomepageButton.setOnClickListener(this);

        ImageButton mMessageButton = (ImageButton) findViewById(R.id.homepage_message_imageButton);
        mMessageButton.setOnClickListener(this);

        FloatingActionButton mSendButton = (FloatingActionButton) findViewById(R.id.homepage_send_floatingActionButton);
        mSendButton.setOnClickListener(this);

        mUserInfoPresenter = new UserInfoPresenter(this);
        mUserInfoPresenter.getUserInfo(MyApplication.getUser().getUserId());


    }

    private void refreshing() {
        if (!MyUtils.isNetworkConnected(HomepageActivity.this)) {
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(HomepageActivity.this, "无网络！", Toast.LENGTH_SHORT).show();
                }
            });
            swipeRefresh.setRefreshing(false);
        } else {
            mPresenter.sendRequest();
        }
    }

    @Override
    public void sendCard() {
        Intent intent = new Intent(HomepageActivity.this, SendActivity.class);
        startActivity(intent);
    }

    @Override
    public void lookPersonalInfo() {
        Intent intent = new Intent(HomepageActivity.this, PersonalHomepageActivity.class);
        startActivity(intent);
    }

    @Override
    public void lookMessage() {
        Intent intent = new Intent(HomepageActivity.this, MessageActivity.class);
        startActivity(intent);
    }

    @Override
    public void enterCard(String tag) {
        Intent intent = new Intent(HomepageActivity.this, BrowseActivity.class);
        intent.putExtra("tag", tag);
        startActivity(intent);
    }

    @Override
    public void setTabsName(List<String> tags) {
        mCustomTagList = tags;

    }

    @Override
    public void setCard(List<CardVO> cardList) {
        swipeRefresh.setRefreshing(false);
        if (cardList != null) {
            mHomepageFragmentList.clear();
            //更新卡片
            for (int i = 0; i < cardList.size(); i++) {
                HomepageFragment homepageFragment =
                        new HomepageFragment();
                homepageFragment.setCard(cardList.get(i), mCustomTagList.get(i));
                mHomepageFragmentList.add(homepageFragment);
            }
            mMyFragmentAdapter.notifyDataSetChanged(true);
            //更新底部TabLayout
            for (int i = 0; i < mCustomTagList.size(); i++) {
                if (i <= 3) {
                    mTabLayout.getTabAt(i).setIcon(mTabImageIdList.get(i));
                } else {
                    mTabLayout.getTabAt(i).setText(mCustomTagList.get(i));
                }
            }
        }
    }

    @Override
    public void getCardError(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        swipeRefresh.setRefreshing(false);
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.homepage_message_imageButton:
                lookMessage();
                break;
            case R.id.homepage_personal_imageButton:
                lookPersonalInfo();
                break;
            case R.id.homepage_send_floatingActionButton:
                sendCard();
                break;
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mPresenter.clearDisposable();
    }

    @Override
    public HomepagePresenter getInstance() {
        return new HomepagePresenter();
    }

    //get personal info
    @Override
    public void showData(ModifyUserInfoResult modifyUserInfoResult) {

        if (modifyUserInfoResult.getUser().getImageUrl() != null){
            MyApplication.getUser().setImageUrl(modifyUserInfoResult.getUser().getImageUrl());
            //Log.d("TEST_" + String.valueOf(modifyUserInfoResult.getUser().getImageUrl()));
        }

        if (modifyUserInfoResult.getUser().getNickName() != null) {
            MyApplication.getUser().setNickName(modifyUserInfoResult.getUser().getNickName().replace("\"",""));
        }

        if (modifyUserInfoResult.getUser().getSex() == 0){
            MyApplication.getUser().setSex(0);
        }else {
            MyApplication.getUser().setSex(1);
        }

        if (modifyUserInfoResult.getUser().getSignature() != null){
            MyApplication.getUser().setSignature(modifyUserInfoResult.getUser().getSignature().replace("\"",""));
        }

    }

    @Override
    public void showModifyResult(ModifyUserInfoResult modifyUserInfoResult) {

    }

    @Override
    public void showInfo(String info) {

    }
}

